package com.sfeir.fastcall.activity;

import java.util.ArrayList;
import java.util.Collections;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.sfeir.fastcall.R;
import com.sfeir.fastcall.access.ContactProvider;
import com.sfeir.fastcall.model.PhoneEntry;
/**
 * Created by :
 * 
 * 		@author SFEIR
 */
public class ActivityListContacts extends ListActivity {

	private ListView mContactList;
	private TextView titre;
	private LayoutInflater inflater;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
		setContentView(R.layout.contact_manager);		
		mContactList = (ListView) findViewById(android.R.id.list);	

		titre = (TextView) findViewById(R.id.filtreContacts);
		inflater = (LayoutInflater) ActivityListContacts.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		populateContactList();     


	}

	private class ContactRow {
		TextView contactText;
		TextView phoneNumberText;
		ImageView phoneNumberType;
	}

	private void populateContactList() {
		final Bundle extras = getIntent().getExtras();
		final String filtre = extras.getString("filtre");	
		titre.setText(filtre);
		// Build adapter with contact entries
		final ArrayList<PhoneEntry> phoneEntries = ContactProvider.getAPI().filtreContacts(filtre);
		Collections.sort(phoneEntries);
		setListAdapter(new BaseAdapter() {

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				ContactRow row = null;
				if(convertView == null){
					row = new ContactRow();
					convertView = inflater.inflate(R.layout.contact_entry, null);
					row.contactText = (TextView) convertView.findViewById(R.id.contactText);
					row.phoneNumberText = (TextView) convertView.findViewById(R.id.phoneNumberText);
					row.phoneNumberType = (ImageView) convertView.findViewById(R.id.phoneNumberType);
					convertView.setTag(row);
				} else {
					row = (ContactRow) convertView.getTag();
				}

				row.contactText.setText(phoneEntries.get(position).displayName);
				row.phoneNumberText.setText(phoneEntries.get(position).phoneNumber);
				if(phoneEntries.get(position).isMobile){
					row.phoneNumberType.setImageResource(R.drawable.p_mobile);
				} else {
					row.phoneNumberType.setImageResource(R.drawable.p_home);
				}

				return convertView;
			}

			@Override
			public long getItemId(int position) {
				return position;
			}

			@Override
			public Object getItem(int position) {
				return phoneEntries.get(position);
			}

			@Override
			public int getCount() {
				return phoneEntries.size();
			}
		});
		mContactList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView parent, View viex, int position, long id) {
				doCall(phoneEntries.get(position));
			}
		});
	}

	private void doCall(PhoneEntry phoneEntry){
		Toast.makeText(this, getText(R.string.call_to)+" "+phoneEntry.phoneNumber, Toast.LENGTH_SHORT).show();
		startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ phoneEntry.phoneNumber)));		
	}
}
